﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Controls;
using System.Collections.ObjectModel;
using System.Windows;
using System.Windows.Markup;

namespace Sinacor.Infra.UI.Controls.SmartClient
{
    public abstract class ExplorerView : ContentControl
    {
        public ExplorerView()
        {
        }

        protected internal abstract IEnumerable<ExplorerViewDescription> GetViewDescriptions();
    }

    public class ExplorerViewCollection : ObservableCollection<ExplorerView>
    {
        private SinacorExplorer _explorer;

        internal ExplorerViewCollection(SinacorExplorer explorer)
        {
            _explorer = explorer;
        }

        protected override void InsertItem(int index, ExplorerView item)
        {
            _explorer.AddViewDescriptions(item.GetViewDescriptions());

            base.InsertItem(index, item);

            _explorer.SelectedView = _explorer.GetFirstViewDescriptionName();
        }

        protected override void SetItem(int index, ExplorerView item)
        {
            _explorer.RemoveViewDescriptions(this[index].GetViewDescriptions());
            _explorer.AddViewDescriptions(item.GetViewDescriptions());

            bool select = this[index].IsVisible;

            base.SetItem(index, item);

            if (select)
                _explorer.SelectedView = _explorer.GetFirstViewDescriptionName(item);
        }

        protected override void RemoveItem(int index)
        {
            _explorer.RemoveViewDescriptions(this[index].GetViewDescriptions());

            bool select = this[index].IsVisible;

            base.RemoveItem(index);

            if (select)
                _explorer.SelectedView = _explorer.GetFirstViewDescriptionName();
        }

        protected override void ClearItems()
        {
            foreach (ExplorerView item in this)
                _explorer.RemoveViewDescriptions(item.GetViewDescriptions());

            base.ClearItems();

            _explorer.SelectedView = null;
        }

        public ExplorerView this[string name]
        {
            get
            {
                return this.FirstOrDefault(v => v.GetViewDescriptions().Any(d => d.Name == name));
            }
        }
    }

    public class ExplorerViewDescription : DependencyObject
    {
        public ExplorerViewDescription(string name, int sortIndex, ExplorerView view)
        {
            this.Name = name;
            this.SortIndex = sortIndex;
            this.View = view;
        }

        public ExplorerViewDescription(string name, object header, int sortIndex, ExplorerView view)
            : this(name, sortIndex, view)
        {
            this.Header = header;
        }

        public static readonly DependencyPropertyKey NameKey = DependencyProperty.RegisterReadOnly("Name", typeof(string), typeof(ExplorerViewDescription), new FrameworkPropertyMetadata(null));
        public static readonly DependencyProperty NameProperty = NameKey.DependencyProperty;

        public string Name
        {
            get { return (string)GetValue(NameProperty); }
            private set { SetValue(NameKey, value); }
        }

        public static readonly DependencyProperty HeaderProperty = DependencyProperty.Register("Header", typeof(object), typeof(ExplorerViewDescription), new FrameworkPropertyMetadata(null));

        public object Header
        {
            get { return (object)GetValue(HeaderProperty); }
            set { SetValue(HeaderProperty, value); }
        }

        public static readonly DependencyProperty SortIndexProperty = DependencyProperty.Register("SortIndex", typeof(int), typeof(ExplorerViewDescription), new FrameworkPropertyMetadata(-1));

        public int SortIndex
        {
            get { return (int)GetValue(SortIndexProperty); }
            set { SetValue(SortIndexProperty, value); }
        }

        public static readonly DependencyPropertyKey ViewKey = DependencyProperty.RegisterReadOnly("View", typeof(ExplorerView), typeof(ExplorerViewDescription), new FrameworkPropertyMetadata(null));
        public static readonly DependencyProperty ViewProperty = ViewKey.DependencyProperty;

        public ExplorerView View
        {
            get { return (ExplorerView)GetValue(ViewProperty); }
            private set { SetValue(ViewKey, value); }
        }
    }
}
